import { FormSchema } from '@/components/Form';
import { useDictStore } from '@/store/modules/dict';
import { renderOptionTag } from '@/utils';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { ShopOutlined } from '@vicons/antd';
import { NIcon, NTag } from 'naive-ui';
import { h, ref } from 'vue';

const dict = useDictStore();

export const schemas = ref<FormSchema[]>([
  {
    field: 'memberId',
    component: 'NInput',
    label: '用户ID',
    componentProps: {
      placeholder: '请输入用户ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'creditGroup',
    component: 'NSelect',
    label: '组别',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择变动的组别',
      options: dict.getOption('creditGroup'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'remark',
    component: 'NInput',
    label: '备注',
    componentProps: {
      placeholder: '请输入备注',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'ip',
    component: 'NInput',
    label: '操作人IP',
    componentProps: {
      placeholder: '请输入操作人IP',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'id',
    component: 'NInput',
    label: '变动ID',
    componentProps: {
      placeholder: '请输入变动ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '变动时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

export const columns = [
  {
    title: '变动ID',
    key: 'id',
    width: 80,
  },
  {
    title: '用户ID',
    key: 'memberId',
    width: 80,
    render(row) {
      return row.memberId == 0 ? '-' : row.memberId;
    },
  },
  {
    title: '用户名称',
    key: 'memberId',
    width: -1,
    render(row) {
      if (row.adminMemberRoleId === 210) {
        return row.adminMemberNickname;
      } else {
        return row.adminMemberRealName ? row.adminMemberRealName : row.adminMemberNickname;
      }
    },
  },
  {
    title: '门店名称',
    key: 'memberId',
    width: 220,
    render(row) {
      return row.storeName;
    },
  },
  {
    title: '身份',
    key: 'adminMemberRoleId',
    align: 'left',
    width: 150,
    render(row) {
      var v1
      var v2

      if (row.adminMemberMemberStatus === 2) {
        v1 = h(NTag, { type: 'warning', bordered : false }, { default: () => '会员' });
      } else {
        v1 = h(NTag, { type: 'success', bordered : false }, { default: () => '用户' });
      }

      if (row.adminMemberRoleId === 1) {
        v1 = ''
        v2 = h(NTag, { type: 'default', bordered : false }, { 
          default: () => [
            // h(NIcon, { style: { marginRight: '4px' } }, { default: () => h(HomeOutlined) }),
            '公司'
          ]
        });
      } else if (row.adminMemberRoleId === 211) {
        v2 = h(NTag, { type: 'info', bordered : false }, { default: () => '销氪' });
      } else if (row.adminMemberRoleId === 100 || row.storeId !== 0) {
        v1 = ''
        v2 = h(NTag, { type: 'default', bordered : false }, {
          default: () => [
            h(NIcon, { style: { marginRight: '4px' } }, { default: () => h(ShopOutlined) }),
            '门店'
          ]
        });
      } else if (row.adminMemberRoleId === 212) {
        v2 = h(NTag, { type: 'error', bordered : false }, { default: () => '服务商' });
      }

      return h('div', {}, [v1, ' ',v2]);
    },
  },
  {
    title: '变动类型',
    key: 'creditType',
    align: 'center',
    render(row) {
      return renderOptionTag('creditType', row.creditType);
    },
    width: 80,
  },
  {
    title: '组别',
    key: 'creditGroup',
    render(row) {
      return renderOptionTag('creditGroup', row.creditGroup);
    },
    align: 'center',
    width: 100,
  },
  {
    title: '变动前',
    align: 'center',
    key: 'beforeNum',
    width: 100,
    render(row) {
      return Number(row.beforeNum).toFixed(2);
    },
  },
  {
    title: '变动数量',
    key: 'num',
    width: 100,
    align: 'center',
    render(row) {
      return Number(row.num).toFixed(2);
    },
  },
  {
    title: '变动后',
    key: 'afterNum',
    align: 'center',
    width: 100,
    render(row) {
      return Number(row.afterNum).toFixed(2);
    },
  },
  {
    title: '备注',
    key: 'remark',
    width: 200,
  },
  // {
  //   title: '操作人IP',
  //   key: 'ip',
  //   width: 150,
  // },
  {
    title: '关联ID',
    key: 'mapId',
    width: 80,
    align: 'center',
    render(row) {
      if (row.mapId === 0) {
        return '-';
      }
      return row.mapId;
    },
  },
  {
    title: '变动时间',
    key: 'createdAt',
    width: 180,
  },
];

export function loadOptions() {
  dict.loadOptions(['creditType', 'creditGroup']);
}
